package com.growthsee.rider.controller;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.growthsee.common.core.controller.BaseController;
import com.growthsee.common.core.domain.AjaxResult;
import com.growthsee.common.utils.SecurityUtils;
import com.growthsee.rider.domain.RiderAnswer;
import com.growthsee.rider.domain.RiderAnswerQuestion;
import com.growthsee.rider.domain.dto.RiderAnswerDto;
import com.growthsee.rider.domain.dto.RiderAnswerQuestionDto;
import com.growthsee.rider.domain.dto.RiderQuestionDto;
import com.growthsee.rider.enumeration.StatusEnum;
import com.growthsee.rider.enumeration.YesNoEnum;
import com.growthsee.rider.service.RiderAnswerQuestionService;
import com.growthsee.rider.service.RiderAnswerService;
import com.growthsee.rider.utils.BeanUtil;
import com.growthsee.rider.utils.BeanUtilsExt;
import com.growthsee.rider.wrapper.QuestionWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author csy
 * @since 2021-05-13
 */
@Api("答题管理")
@RestController
@RequestMapping("/rider/answerQuestion")
@AllArgsConstructor
public class RiderAnswerQuestionController extends BaseController {

    private final RiderAnswerService riderAnswerService;
    private final RiderAnswerQuestionService riderAnswerQuestionService;
    @ApiOperation(value = "答题操作", notes = "答题操作", response = RiderAnswerQuestion.class)
    @PostMapping("{courseId}")
    public AjaxResult insert(@ApiParam(name = "课程id", required = true) @PathVariable String courseId, @RequestBody List<RiderAnswerQuestion> answerQuestions ) {
        if(CollectionUtils.isEmpty(answerQuestions))return AjaxResult.error("答卷不能为空");

        RiderAnswer riderAnswer = new RiderAnswer();
        riderAnswer.setCourseId(courseId);
        riderAnswer.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        riderAnswer.setCreateTime(new Date());
        riderAnswer.setUserId(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        riderAnswer.setDeleted(YesNoEnum.NO.getValue());
        riderAnswer.setStatus(StatusEnum.ENABLE.getValue());
        riderAnswerService.save(riderAnswer);
        answerQuestions.forEach(v->{
            v.setAnswerId(riderAnswer.getId());
        });
        riderAnswerQuestionService.saveBatch(answerQuestions);

        RiderAnswerDto riderAnswerDto = new RiderAnswerDto();
        BeanUtil.copyProperties(riderAnswer,riderAnswerDto);
        List<RiderAnswerQuestionDto>questionDtos= BeanUtilsExt.copyList(answerQuestions, RiderAnswerQuestionDto.class);
        riderAnswerDto.setAnswerQuestionDtos(questionDtos);
        QuestionWrapper.builder().audit(riderAnswerDto);
        return AjaxResult.success(riderAnswerDto);
    }
}

